package com.sunday.common.core.lambda.toolkit.support;

import com.sunday.common.core.constants.StringConstants;
import com.sunday.common.core.utils.ClassUtils;
import com.sunday.common.core.utils.ReflectToJsonUtils;

/**
 * 因为存在多个SerializedLambda。为了保持获取的一致性，创建了该类
 * {@link java.lang.invoke.SerializedLambda} 和 {@link com.sunday.common.core.lambda.toolkit.support.SerializedLambda} 的返回值
 * implMethodName       -> lambda 表达式对应的实现方法名称
 * instantiatedClass    -> 实例化该方法的类
 *
 * @author zsy
 * @see com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta
 * @since 2024/1/16
 */
public record LambdaMeta(String implMethodName, Class<?> instantiatedClass) {

    public static LambdaMeta of(String implMethodName, String instantiatedMethodType) {
        String instantiatedType = instantiatedMethodType.substring(2, instantiatedMethodType.indexOf(StringConstants.SEMICOLON)).replace(StringConstants.SLASH, StringConstants.DOT);
        return new LambdaMeta(implMethodName, ClassUtils.forName(instantiatedType));
    }

    @Override
    public String toString() {
        return ReflectToJsonUtils.toJson(this, true);
    }

}
